/*  PrimeBlaster
    by Kjetil Eide 
    kjetile445@gmail.com

    This file is part of PrimeBlaster

    PrimeBlaster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PrimeBlaster is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrimeBlaster.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.eide.spaceship;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class TitleScreen extends BasicGameState {
	
	/** The ID given to this state */
	public static final int ID = 1;
	
	/** Title font */
	private Font titleFont;
	private final static String title = "Prime Blaster";
	private final static String credits = "by Kjetil Eide";
	private final static String version = "version " + PrimeBlaster.VERSION;
	
	/** The font to write the message with */
	private Font font;
	/** The menu options */
	private String[] options = new String[] { "Start Game", "Exit" };
	/** The index of the selected option */
	private int selected;
	/** The game holding this state */
	private StateBasedGame game;

	private final int NUMBER_OF_STARS = 400;
	private StarField starField;
	
	private Music music;
	
	public TitleScreen(PlayerStatus playerStatus) {

	}

	@Override
	public int getID() {
		return ID;
	}

	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		font = new AngelCodeFont("spacedata/demo2.fnt", "spacedata/demo2_00.tga");
		titleFont = new AngelCodeFont("spacedata/hiero.fnt", "spacedata/hiero.png");
		this.game = game;
		music = new Music("spacedata/title_music.ogg", false);
		starField = new StarField(NUMBER_OF_STARS, PrimeBlaster.SCREEN_WIDTH, PrimeBlaster.SCREEN_HEIGHT);
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		starField.drawStars(g);
		g.setColor(Color.white);
		g.drawString(credits,(container.getWidth() - 120) / 2, 110);
		g.drawString(version, (container.getWidth() - 100) / 2, container.getHeight()-50);
		g.setFont(titleFont);
		g.drawString(title, (container.getWidth()-titleFont.getWidth(title))/2, 50);
	
		g.setFont(font);
		g.setColor(Color.white);

		for (int i = 0; i < options.length; i++) {
			g.drawString(options[i], 400 - (font.getWidth(options[i]) / 2),
					200 + (i * 50));
			if (selected == i) {
				g.drawRect(200, 190 + (i * 50), 400, 50);
			}
		}
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta) {
		starField.updateStars(delta);
	}

	/**
	 * @see org.newdawn.slick.state.BasicGameState#keyReleased(int, char)
	 */
	public void keyReleased(int key, char c) {
		if (key == Input.KEY_DOWN) {
			selected++;
			if (selected >= options.length) {
				selected = 0;
			}
		}
		if (key == Input.KEY_UP) {
			selected--;
			if (selected < 0) {
				selected = options.length - 1;
			}
		}
		
		if (key == Input.KEY_ENTER) {
			switch(selected) {
			case 0: //game.enterState(LevelGetReady.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
				    music.stop();
				    game.enterState(LevelGetReady.ID);
					break;
			case 1: System.exit(0);
					
			}
		}
	}
	
	public void enter(GameContainer c, StateBasedGame game) throws SlickException {
		super.enter(c, game);
		music.play();
		music.loop();
	}
}
